package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.SysCompanies;
import com.ruoyi.system.mapper.SysCompaniesMapper;
import com.ruoyi.system.service.ISysCompaniesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 地市级公司信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-18
 */
@Service
public class SysCompaniesServiceImpl implements ISysCompaniesService {

    private static final Logger log = LoggerFactory.getLogger(SysCompaniesServiceImpl.class);

    @Autowired
    private SysCompaniesMapper sysCompaniesMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询地市级公司信息
     *
     * @param companiesId 地市级公司信息主键
     * @return 地市级公司信息
     */
    @Override
    public SysCompanies selectSysCompaniesByCompaniesId(Long companiesId) {
        return sysCompaniesMapper.selectSysCompaniesByCompaniesId(companiesId);
    }

    /**
     * 查询地市级公司信息列表
     *
     * @param sysCompanies 地市级公司信息
     * @return 地市级公司信息
     */
    @Override
    public List<SysCompanies> selectSysCompaniesList(SysCompanies sysCompanies) {
        return sysCompaniesMapper.selectSysCompaniesList(sysCompanies);
    }

    /**
     * 新增地市级公司信息
     *
     * @param sysCompanies 地市级公司信息
     * @return 结果
     */
    @Override
    public int insertSysCompanies(SysCompanies sysCompanies) {
        sysCompanies.setCreateTime(DateUtils.getNowDate());
        return sysCompaniesMapper.insertSysCompanies(sysCompanies);
    }

    /**
     * 修改地市级公司信息
     *
     * @param sysCompanies 地市级公司信息
     * @return 结果
     */
    @Override
    public int updateSysCompanies(SysCompanies sysCompanies) {
        sysCompanies.setUpdateTime(DateUtils.getNowDate());
        return sysCompaniesMapper.updateSysCompanies(sysCompanies);
    }

    /**
     * 批量删除地市级公司信息
     *
     * @param companiesIds 需要删除的地市级公司信息主键
     * @return 结果
     */
    @Override
    public int deleteSysCompaniesByCompaniesIds(String companiesIds) {
        return sysCompaniesMapper.deleteSysCompaniesByCompaniesIds(Convert.toStrArray(companiesIds));
    }

    /**
     * 删除地市级公司信息信息
     *
     * @param companiesId 地市级公司信息主键
     * @return 结果
     */
    @Override
    public int deleteSysCompaniesByCompaniesId(Long companiesId) {
        return sysCompaniesMapper.deleteSysCompaniesByCompaniesId(companiesId);
    }

    /**
     * 数据导入
     *
     * @param list            导入的数据列表
     * @param isUpdateSupport 是否更新已有数据
     * @param operName        操作人的名字
     * @return 结果
     */
    @Override
    public String importCompanies(List<SysCompanies> list, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysCompanies obj : list) {
            try {
                // 验证是否存在这个公司
                SysCompanies c = sysCompaniesMapper.selectSysCompaniesByCompaniesId(obj.getCompaniesId());
                if (StringUtils.isNull(c)) {
                    BeanValidators.validateWithException(validator, obj);
                    obj.setCreateBy(operName);
                    this.insertSysCompanies(obj);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、公司 " + obj.getCompaniesName() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, obj);
                    obj.setUpdateBy(operName);
                    this.updateSysCompanies(obj);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、公司 " + obj.getCompaniesName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、公司 " + obj.getCompaniesName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、公司 " + obj.getCompaniesName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
